import { request } from '../request';
import Taro from '@tarojs/taro';
import { getRequestUrl } from '../request/helpers';
import { localStg } from '@/utils';
// import { randomUUID } from 'crypto';

export async function verifyIdCard(dictCode: string, name:string) {
    // const { error, success } = await request.get<any>(`/sys/dict/list?dictCode=${dictCode}`, {}, {
    //     useErrMsg: true
    // });
    // const id = success?.records[0]?.id

    // if (!error && id) {
    //     const { error, success } = await request.get<any>(`/sys/dictItem/list?dictId=${id}`, {}, {
    //         useErrMsg: true
    //     });

    //     if (!error && success?.records) return success?.records as Array<dictItem>
    // }
    return true
}

export function sendSmsCode(num: string) {
    return request.get<any>(`/WeChat/sendMsg?phoneNumberSet=${num}&templateParamSet=88529`, {}, {
        useErrMsg: true
    });
}

export function verifyPhone(phone: string, code:string, openid: string) {
    return request.get<any>(`/WeChat/verifyCode?inputCode=${code}&phoneNumber=${phone}&openId=${openid}`, {}, {
        useErrMsg: true
    });
}

// 获取图形验证码
export async function getCaptcha() {
    try {
        const token = localStg.get('token');
        const headers: any = {
            'Content-Type': 'application/json',
        };
        
        if (token) {
            headers['X-Access-Token'] = token;
        }
        
        const res = await Taro.request({
            url: getRequestUrl('/sys_captcha/sysCaptcha/image'),
            method: 'GET',
            responseType: 'arraybuffer',
            header: headers
        });
        
        if (res.statusCode === 200) {
            // 将 arraybuffer 转为 base64
            const base64 = Taro.arrayBufferToBase64(res.data);
            // 获取响应头中的 Captcha-UUID
            const captchaUUID = res.header['Captcha-UUID'] || res.header['captcha-uuid'];
            
            return {
                error: null,
                success: {
                    imageUrl: `data:image/png;base64,${base64}`,
                    uuid: captchaUUID
                }
            };
        } else {
            return {
                error: {
                    message: '获取验证码失败',
                    errorCode: res.statusCode
                },
                success: null
            };
        }
    } catch (error: any) {
        return {
            error: {
                message: error.message || '获取验证码失败',
                errorCode: -1
            },
            success: null
        };
    }
}

// 验证图形验证码
export function verifyCaptcha(captchaCode: string, captchaUUID: string, phone: string) {
    return request.get<any>('/sys_captcha/sysCaptcha/validate', {
        code: captchaCode,
        uuid: captchaUUID,
        phoneNumberSet: phone
    }, {
        useErrMsg: true
    });
}

export async function verifyFace(num: string) {
    return true;
}

export async function verifyHealthCode(num: string) {
    return true;
}



